layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startTime'
        	,value:new Date()
    });
    
    laydate.render({
    	elem:'#endTime'
        	,value:new Date()
    });

    laydate.render({
    	elem:'#startTime2'
    });
    
    laydate.render({
    	elem:'#endTime2'
    });

    /**
     * Inbound Order Management
     */
    var OrderPut = {
        tableId: "orderPutTable"
    };

    /**
     * Inbound Order Detail Management
     */
    var OrderPutDetailed = {
        tableId: "orderPutDetailedTable"
    };

    /**
     * Initialize table columns
     */
    OrderPut.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'billstate', title: 'Status',templet: '#billstateTpl'},
            {field: 'putCustomerName', title: 'Customer'},
            {field: 'warehouseName', title: 'Warehouse'},
            {field: 'putNumber', title: 'System Order No', style:'color: #2b85e4;font-weight: 600;'},
            {field: 'putCustomerNumber',width:150, title: 'Customer Order No', style:'color: #2b85e4;font-weight: 600;'},
            {field: 'putType', title: 'Inbound Type'},
            {field: 'putMode', title: 'Inbound Method', totalRowText: 'Total:'},
            {field: 'detailedPutnum', title: 'Quantity', totalRow: true},
            {field: 'detailedPutvolume', title: 'Volume', totalRow: true},
            {field: 'detailedPutweight', title: 'Weight', totalRow: true},
            {field: 'detailedMoney', title: 'Amount', totalRow: true},
            {field: 'createName', title: 'Created By'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'submissionName', title: 'Submitted By'},
            {field: 'submissionTime', sort: true, title: 'Submission Time'},
            {field: 'completeTime', sort: true, title: 'Completion Time'},
            {field: 'notes', title: 'Remarks', style:'color: #FF4500;font-weight: 600;'}
        ]];
    };

    
    /**
     * Click search button
     */
    OrderPut.search = function () {
        var queryData = {};
        queryData['billstate'] = $("#billstate").val();
        queryData['putCustomerId'] = $("#putCustomerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['condition'] = $("#condition").val();
        queryData['startTime2'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['startTime2'] = $("#startTime2").val();
        queryData['endTime2'] = $("#endTime2").val();
        table.reload(OrderPut.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // Batch Print click event
    $('#btnPrint').click(function () {
        var checkStatus = table.checkStatus('orderPutTable');
        data2 = checkStatus.data;
        if (data2.length>0) {
            var ids = [];
            for (var i = 0; i < data2.length; i++) {
                ids.push(data2[i].id);
            }
            layer.open({
                type: 2,
                title: 'Batch Print Inbound Orders',
                area: ['990px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/orderPut/print?ids='+ids,
                end: function () {
                }
            });
        } else {
            layer.alert('Please select at least one record');
        }
    });
    
    // Render table
    var tableResult = table.render({
        elem: '#' + OrderPut.tableId,
        url: Feng.ctxPath + '/orderPut/querylist',
        page: true,
        limit: 60,
		limits:[40,60,120,240,360,480,1200],
        height: 410,
        totalRow: true ,//Enable total row
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cols: OrderPut.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderPut.search();
    });
    
    /**
     * Initialize table columns
     */
    OrderPutDetailed.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'ID'},
            {field: 'detailedFactory', title: 'Factory No', hide: true},
            {field: 'recommend', title: 'Inbound Batch'},
            {field: 'detailedGoodsname', title: 'Product Name', style:'background-color: #dbf1e1;font-weight: 600;',width: 270},
            {field: 'goodsNumber', title: 'Product Code', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', title: 'SKU No', style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:'},
            {field: 'detailedPutnum', title: 'Expected Qty', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedDateofmanufacture', title: 'Manufacturing Date', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedBatchnumber', title: 'Batch No', style:'background-color: #dbf1e1;font-weight: 600;',width: 140},
            {field: 'detailedPutweight', title: 'Expected Weight', totalRow: true},
            {field: 'detailedPutvolume', title: 'Expected Volume', totalRow: true},
            {field: 'detailedUnitprice', title: 'Unit Price'},
            {field: 'detailedMoney', title: 'Amount', totalRow: true},
            {field: 'detailedLargeclass', title: 'Category'},
            {field: 'detailedGoodstype', title: 'Type', hide: true},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'detailedPacking', title: 'Packaging'},
            {field: 'goodsSpecs', title: 'Specifications'}
        ]];
    };

    // 单击行事件
    table.on('row(' + OrderPut.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + OrderPut.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + OrderPut.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
        orderPut(data);
    });
    
    // 工具条点击事件
    table.on('tool(' + OrderPut.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'launch') {
        	orderPut.launch(data);
        } else if (layEvent === 'revoke') {
        	orderPut.revoke(data);
        } 
    });
    
    function orderPut(data){
    	var putNumber;
    	if (data!=null) {
    		$("#putDetailedNumber").val(data.putNumber);
    		putNumber = data.putNumber;
		} else {
			putNumber = 'nodata';
		}

        /**
         * Click search button
         */
    	OrderPutDetailed.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            table.reload(OrderPutDetailed.tableId, {
                where: queryData, page: {curr: 1},
                 done:function () {
                     // 搜索按钮点击事件
                     $('#btnSearchDetailed').click(function () {
                    	 OrderPutDetailed.search();
                     });
                }
            });
        };
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + OrderPutDetailed.tableId,
            url: Feng.ctxPath + '/orderPutDetailed/list?putDetailedNumber=' + putNumber,
            page: true,
            limit: 30,
            height: 400,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: OrderPutDetailed.initColumn()
        });

        // 搜索按钮点击事件
        $('#btnSearchDetailed').click(function () {
        	OrderPutDetailed.search();
        });
    };
    
});
